<?php

/*
 * 管理员操作
 */

class Admin_AdminuserController extends Edm_Controller_Action {

    public function indexAction() {
        $this->view->menu = array('添加管理员' => '/admin/adminuser/add');
        $this->view->ur_here = '管理员列表';

        $filter = array('pageNum' => 1, 'pageSize' => $this->common->defaultPageSize);
        /* 读取数据，列表 */

        if ($this->_request->getParam('page')) {
            $filter['pageNum'] = $this->_request->getParam('page');
        }
        if ($this->_request->get('keywords')) {
            $filter['keywords'] = $this->_request->get('keywords');
        }

        $userlist = $this->_admin->userList($filter);
        $this->view->userlist = $userlist['res'];
        $this->pager->Pager($userlist['recordCount'], $filter['pageSize']);
        $this->view->pageResult = $this->pager->display();
        $this->view->uri = $this->common->getUrl();
    }

    /* jquery 验证密码,用户名  */

    public function validatorAction() {
        if ($this->_request->getParam('admin_id') && $this->_request->getParam('password')) {
            $user = array('admin_id' => $this->_request->getParam('admin_id'), 'password' => $this->_request->getParam('password'));
            die($this->_admin->userExists($user, 'password'));
        } elseif ($this->_request->getParam('admin_name')) {
            $user = array('admin_name' => $this->_request->getParam('admin_name'));
            die($this->_admin->userExists($user));
        }
        exit();
    }

    /* 添加、修改管理员 */

    public function addAction() {

        /* 处理提交 */
        $data = array();
        $errors = array();
        $request = $this->_request;
        $act = $this->_request->getParam('act');
        /* 如果是编辑、保存 */
        if ($act == 'edit') {
            $admin_id = $this->_request->getParam('admin_id');
            $userInfo = $this->_admin->userInfo($admin_id);
            $userInfo['uri'] = $this->_request->getParam('fromurl');
            $userInfo['act'] = $act;
            $this->view->userInfo = $userInfo;
        }

        if ($request->isPost()) {
            $data = array(
                'admin_name' => $request->getPost('admin_name'),
                'password' => $request->getPost('user_password'),
                'email' => $request->getPost('user_email'),
                'desc' => $request->getPost('user_desc'),
                'role' => 'administrator'
            );
            $user_password2 = $request->getPost('user_password2');
            $user_password_old = $request->getPost('user_password_old');
            $data['password'] = md5($data['password']);
            $uri = $request->getPost('uri');

            if (!$act) { /* 如果是添加 */
                /* 是否存在 */
                if (!$this->_admin->userExists(array('admin_name' => $data['admin_name']))) {
                    $res = $this->_admin->userAdd($data);
                    $this->common->msg(urldecode($uri), '信息添加成功！');
                } else {
                    $this->common->msg(urldecode($uri), '此用户名已经存在！', 'Warning');
                }
            } else { /* 修改 */
                $admin_id = $request->getPost('admin_id');
                unset($data['admin_name'], $data['add_time']);
                $this->_admin->userUpdate($data, $admin_id);
                $this->common->msg($uri, '信息更新成功！');
            }
        }
        $this->view->menu = array('管理员列表' => '/admin/adminuser/');
        $this->view->ur_here = '添加/修改管理员信息';
        $this->view->uri = $this->common->getUrl();
    }

    /*
     * 根据admin_id删除信息
     */

    public function delAction() {
        /* 禁用视图 */
        $this->_helper->viewRenderer->setNoRender();
        $request = $this->getRequest();
        if ($this->_request->getParam('admin_id') && $this->_request->getParam('fromurl')) {
            $admin_id = $this->_request->getParam('admin_id');
            $fromUrl = $this->_request->get('fromurl');

            if ($this->_admin->userDelete($admin_id)) {
                $this->common->msg($fromUrl, '信息删除成功！');
            } else {
                $this->common->msg($fromUrl, '信息删除时出现异常,删除失败！', 'Error');
            }
        }
    }

}